<template>
  <div class="login">
    <div class="mask" @click="$store.commit('modifyShowLogin', false)"></div>
    <div class="login-box">
      <div class="close" @click="$store.commit('modifyShowLogin', false)">
        <img src="../../assets/images/login/userArrow.png" alt="" />
      </div>
      <ul class="login-type flex">
        <li :class="{ active: currentIndex === 0 }" @click="currentIndex = 0">
          手机号码登陆
        </li>
        <li :class="{ active: currentIndex === 1 }" @click="weixinClickFn">
          微信扫码登陆
        </li>
      </ul>
      <div class="phone-login" v-show="currentIndex === 0">
        <div class="form-item">
          <input type="text" placeholder="输入手机号" v-model="phone" />
        </div>
        <div class="form-item">
          <!-- 拖动验证 -->
          <SlideVerify
            :l="40"
            :r="20"
            :w="395"
            :h="140"
            :slider-text="msg"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
            class="slide-box"
            ref="slideBlock"
          ></SlideVerify>
        </div>
        <div class="form-item">
          <input type="text" placeholder="输入验证码" v-model="code" />
          <span class="btn" @click="getVeriryCode"
            >获取验证码<span v-show="time !== 60">({{ time }})</span>
          </span>
        </div>
        <div class="btn big" @click="PhoneLogin">登录</div>
      </div>

      <div class="wechat-login" v-show="currentIndex === 1">
        <div id="weixin"></div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { validateTelephone } from "../../utils/index.js";
import { getVerifyApi, phoneLoginApi } from "../../http/api";
import { toast } from "../../utils/toast.js";
export default {
  data() {
    return {
      currentIndex: 0,
      msg: "向右移动",
      time: 60,
      phone: "",
      //  是否已经滑动验证成功
      isSlideVerfiy: false,
      code: "",
    };
  },
  methods: {
    // 点击微信登陆
    weixinClickFn() {
      this.currentIndex = 1;
      // 就是使用我们在html里面引入js 里面给我们提供下面这个构造函数 new这个构造函数就给我们提供一个可以使用微信扫一下的图片
      const _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要写死 是公司申请 在微信开发者后台申请
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          // 环境变量
          window.btoa(process.env.VUE_APP_STATE_URL + "/#" + _this.$route.path)
        ),
        // 调用样式文件 需要注入自己的CSS样式
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLCAuaW1wb3dlckJveCAuaW5mb3sNCiAgICBkaXNwbGF5OiBub25lOw0KfQ0KDQouaW1wb3dlckJveCAucXJjb2Rlew0KICAgIG1hcmdpbi10b3A6IDIwcHg7DQp9DQoNCg==",
      });
    },
    async PhoneLogin() {
      if (!validateTelephone(this.phone))
        return toast("手机号不正确", "danger");
      if (!this.isSlideVerfiy) return toast("请先拖动滑块，亲亲", "warning");
      if (this.code == "") return toast("验证码为空", "danger");
      const res = await phoneLoginApi({
        verifyCode: this.code,
        phone: this.phone,
      });
      if (res.code === 400) {
        alert("验证码错误");
      }
      if (res.code === 0) {
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        toast(res.message, "success");
        this.$store.commit("modifyShowLogin", false);
      }
    },

    getVeriryCode() {
      // 先验证手机号码  ---用正则表达式
      // alert("手机号不正确"); icon-toast-shibai_huaban
      // console.log(1);
      if (!validateTelephone(this.phone))
        return toast("手机号不正确", "danger");
      if (!this.isSlideVerfiy) return toast("请先拖动滑块，亲亲", "warning");
      if (this.time != 60) return toast("验证码已发送请耐心等候", "danger");

      //   修改事件让倒计时显示
      this.time--;
      const timer = setInterval(() => {
        this.time--;
        if (this.time == 0) {
          this.time = 60;
          clearInterval(timer);
        }
      }, 1000);
      // request.post("/sendSMS", `phone=${this.phone}`).then((res) => {
      //   console.log(res);
      // });
      // axios.post("http://gz.wolfcode.cn/cms/sendSMS", `phone=${this.phone}`, {
      //   headers: { "Content-Type": "application/x-www-form-urlencoded" },
      // }).then((res) => {
      // console.log(res);
      // });
      getVerifyApi(this.phone).then((res) => {
        if (res.code === 0) {
          // 验证码已经发送成功请稍等
          toast("验证码已发送成功", "success");
        }
      });
    },

    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
      this.isSlideVerfiy = true;
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 点击登录按钮
    submitFn(formName) {
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        console.log("请滑动拼图");
      } else {
        console.log("开始登录");
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
/* 在vue里面 如果想从父组件修改 */
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -140px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -140px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
.login {
  background-color: rgba(0, 0, 0, 0.5);
  // 定位
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .login-box {
    width: 555px;
    height: 423px;
    background-image: url(../../assets/images/login/loginBg.png);
    position: absolute;
    top: 50%;
    left: 50%;
    box-sizing: border-box;
    transform: translate(-50%, -50%);
    padding: 40px 80px;
    .close {
      top: 20px;
      right: 60px;
      position: absolute;
      cursor: pointer;
    }
    .login-type {
      justify-content: center;
      li {
        padding: 0 10px;
        cursor: pointer;
        color: #ccc;
        &.active {
          color: #333;
        }
        &:nth-child(2) {
          border-left: 1px solid #ccc;
        }
      }
    }
    .phone-login {
      padding-top: 20px;
      .form-item {
        display: flex;
        margin-bottom: 10px;
        input {
          box-sizing: border-box;
          height: 50px;
          border: 1px solid #ccc;
          flex: 1;
          padding: 0 10px;
        }
      }
      .btn {
        color: #ccc;
        width: 120px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        background-color: #fff;
        margin-left: 10px;
        background-color: #0a328e;
        &.big {
          width: 100%;
          margin: 0;
        }
      }
    }
    .wechat-login {
      #weixin {
        margin: 0 auto;
      }
    }
  }
}
</style>